package com.wanmao.test.framgent;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wanmao.test.MainActivity;
import com.wanmao.test.ProjectListActivity;
import com.wanmao.test.R;
import com.wanmao.test.bean.GCBean;
import com.wanmao.test.bean.GZJLBean;
import com.wanmao.test.bean.ProjectBean;
import com.wanmao.test.bean.RYRWBean;
import com.wanmao.test.bean.SBBean;
import com.wanmao.test.bean.XMBean;
import com.wanmao.test.bean.XSJLBean;
import com.wanmao.test.bean.XZBean;
import com.wanmao.test.bean.YDBean;
import com.wanmao.test.service.GCService;
import com.wanmao.test.service.GZJLService;
import com.wanmao.test.service.ProjectService;
import com.wanmao.test.service.RYRWService;
import com.wanmao.test.service.SBService;
import com.wanmao.test.service.ToolShowDemo;
import com.wanmao.test.service.XMService;
import com.wanmao.test.service.XSJLService;
import com.wanmao.test.service.XZService;
import com.wanmao.test.service.YDService;
import com.wanmao.test.util.DateUtil;
import com.wanmao.test.util.DateUtils;
import com.wanmao.test.util.DialogUtils;
import com.wanmao.test.util.StaticUtil;
import com.wanmao.test.view.BasePageFragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.wanmao.test.util.ToolUtils.getCheckTime;

public class Framgent_Test_Task extends BasePageFragment implements View.OnClickListener {
    private final static String TAG = "获取任务信息";
    private LinearLayout layout_daiban;
    private TextView tv_number, tv_NameShow;
    private ProjectService service;
    private GCService gcService;
    private XZService xzService;
    private XMService xmService;
    private YDService ydService;
    private SBService sbService;
    private XSJLService xsjlService;
    private GZJLService gzjlService;
    private RYRWService ryrwService;
    private ProjectBean bean;
    private List<RYRWBean> list;
    private String JDJD;
    private String ERRORINFO = "";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.framgent_test_task, container, false);
        service = new ProjectService(getActivity());
        gcService = new GCService(getActivity());
        xzService = new XZService(getActivity());
        xmService = new XMService(getActivity());
        ydService = new YDService(getActivity());
        sbService = new SBService(getActivity());
        xsjlService = new XSJLService(getActivity());
        gzjlService = new GZJLService(getActivity());
        ryrwService = new RYRWService(getActivity());
        if (MainActivity.istrue.equals("true")) {
            MyAsynTask myAsynTask = new MyAsynTask(view);
            myAsynTask.execute();
        } else {
            intenView(view);
        }
        return view;

    }

    @Override
    public void onStart() {
        super.onStart();
        setUserVisibleHint(true);
    }

    private void http() {
        Map<String, String> map = new HashMap<String, String>();
        try {

            map.put("ISC_ID", StaticUtil.ISC_ID);
            map.put("APPID", StaticUtil.APPID);
            map.put("RWKSSJ", DateUtil.getTimeStart(getCheckTime()));
            map.put("RWJSSJ", DateUtil.getTime());

            String response = ToolShowDemo.manageWebServices("GetJSJDRWCX", map);
            Log.e(TAG, "http: " + response);
            JSONObject json = null;

            json = JSON.parseObject(response);
            int result = json.getIntValue("result");
            if (result == 0) {
                ERRORINFO = "请确认PMS2.0接口服务";
            } else if (result == 1) {
                JSONArray value = json.getJSONArray("resultValue");
                if (value.size() <= 0) {
                    return;
                }
                List<ProjectBean> list = new ArrayList<>();
                for (int i = 0; i < value.size(); i++) {
                    JSONObject obj = value.getJSONObject(i);
                    bean = new ProjectBean();
                    String RWID = obj.getString("OBJ_ID");
                    String RWMC = obj.getString("HDMC");
                    JDJD = obj.getString("GZJD");
                    String ZYLXBM = obj.getString("ZYLX");
                    String ZYLX = getZYLX(ZYLXBM);
                    String HDSJ = obj.getString("HDSJ");
                    String string = String.valueOf(Long.parseLong(HDSJ) / 1000);
                    final String time = DateUtils.timedate(string + "");
                    int jd = Integer.parseInt(JDJD);
                    bean.setRWID(RWID);
                    bean.setRWMC(RWMC);
                    bean.setJDJD(StaticUtil.list_jd.get(jd - 1));
                    bean.setZYLX(ZYLX);
                    bean.setHDSJ(time);

                    list.add(bean);
                    ProjectBean b = service.findByRWID(bean.getRWID());

                    if (null != b) {
                    } else {
                        service.save(bean);
                        httpProjectDetails(RWID);
                    }
                    RYRWBean ryrwBean = new RYRWBean();
                    ryrwBean.setOBJ_ID(StaticUtil.ISC_ID + RWID);
                    ryrwBean.setRYID(StaticUtil.ISC_ID);
                    ryrwBean.setRWID(RWID);
                    ryrwService.save(ryrwBean);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            ERRORINFO = "请检查pmis服务";
        }
    }


    private void httpProjectDetails(String rwid) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("ISC_ID", StaticUtil.ISC_ID);
        map.put("APPID", StaticUtil.APPID);
        map.put("OBJ_ID", rwid);
        try {


            String response = ToolShowDemo.manageWebServices("GetJSJDRWXX", map);
            JSONObject json = JSON.parseObject(response);
            int result = json.getIntValue("result");

            if (result == 0) {
                String content = json.getString("description");
                ERRORINFO = "任务详情获取失败";
            } else {
                JSONArray value = json.getJSONArray("resultValue");

                if (value.size() <= 0) {
                } else {
                    JSONObject obj = value.getJSONObject(0);
                    String ZYLXBM = obj.getString("ZYLX");
                    String ZYLX = getZYLX(ZYLXBM);
                    String HDNR = obj.getString("HDNR");//任务内容
                    String DZHXL = obj.getString("DZHXL");//电站\线路
                    String DZHXLID = obj.getString("DZHXLID");//工程ID

                    bean.setHDNR(HDNR);
                    bean.setDZHXL(DZHXL);
                    bean.setDZHXLID(DZHXLID);
                    service.update(rwid, HDNR, DZHXL, DZHXLID);
                    httpXSJL(DZHXLID, rwid);
                    httpGZJL(DZHXLID, rwid);
                    httpGC(DZHXLID);
                    httpSB(DZHXLID, rwid);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            ERRORINFO = "请检查pmis服务";
        }

    }


    private void httpXSJL(String BDZXL, String rwid) {
        Map<String, String> map = new HashMap<String, String>();
        try {
            map.put("APPID", StaticUtil.APPID);
            map.put("BDZXL", BDZXL);
            String response = ToolShowDemo.manageWebServices("GetBDXSJL", map);
            JSONObject json = JSON.parseObject(response);
            int result = json.getIntValue("result");
            if (result == 0) {
                String content = json.getString("description");
                ERRORINFO = "巡视记录获取失败";
            } else {
                JSONArray value = json.getJSONArray("resultValue");
                List<XSJLBean> list = new ArrayList<>();
                XSJLBean xsjlBean;
                for (int i = 0; i < value.size(); i++) {
                    JSONObject obj = value.getJSONObject(i);
                    xsjlBean = new XSJLBean();
                    String OBJ_ID = obj.getString("OBJ_ID");
                    String BDZMC = obj.getString("BDZMC");
                    String XSRYMC = obj.getString("XSRYMC");
                    String XSKSSJ = DateUtils.toTimeStr(obj.getString("XSKSSJ"));
                    String XSJSSJ = DateUtils.toTimeStr(obj.getString("XSJSSJ"));
                    String XSLX = obj.getString("XSLX");
                    String XSJG = obj.getString("XSJG");
                    String XSNR = obj.getString("XSNR");
                    xsjlBean.setOBJ_ID(OBJ_ID);
                    xsjlBean.setRWID(rwid);
                    xsjlBean.setBDZMC(BDZMC);
                    xsjlBean.setXSRYMC(XSRYMC);
                    xsjlBean.setXSKSSJ(XSKSSJ);
                    xsjlBean.setXSJSSJ(XSJSSJ);
                    xsjlBean.setXSLX(XSLX);
                    xsjlBean.setXSJG(XSJG);
                    xsjlBean.setXSNR(XSNR);
                    list.add(xsjlBean);
                    xsjlService.save(xsjlBean);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

        }

    }


    private void httpGZJL(String GZJL, String rwid) {
        Map<String, String> map = new HashMap<String, String>();
        try {
            map.put("APPID", StaticUtil.APPID);
            map.put("BDZXL", GZJL);
            String response = ToolShowDemo.manageWebServices("GetGZJL", map);
            JSONObject json = JSON.parseObject(response);
            int result = json.getIntValue("result");
            if (result == 0) {
                String content = json.getString("description");
                ERRORINFO = "故障记录获取失败";
            } else {
                JSONArray value = json.getJSONArray("resultValue");
                List<GZJLBean> list = new ArrayList<>();
                GZJLBean gzjlBean;
                for (int i = 0; i < value.size(); i++) {
                    JSONObject obj = value.getJSONObject(i);
                    gzjlBean = new GZJLBean();
                    String GZXLMC = obj.getString("GZXLMC");
                    String GZLX = obj.getString("GZLX");
                    String DYDJ = obj.getString("DYDJ");
                    String GZFSSJ = DateUtils.toTimeStr(obj.getString("GZFSSJ"));
                    String BDZMC = obj.getString("BDZMC");
                    String SFTY = obj.getString("SFTY");
                    String SFTZ = obj.getString("SFTZ");
                    String OBJ_ID = obj.getString("OBJ_ID");
                    String GZJT = obj.getString("GZJT");
                    String GZQK = obj.getString("GZQK");
                    gzjlBean.setOBJ_ID(OBJ_ID + rwid);
                    gzjlBean.setRWID(rwid);
                    gzjlBean.setBDZMC(BDZMC);
                    gzjlBean.setGZXLMC(GZXLMC);
                    gzjlBean.setGZLX(GZLX);
                    gzjlBean.setDYDJ(DYDJ);
                    gzjlBean.setGZFSSJ(GZFSSJ);
                    gzjlBean.setSFTY(SFTY);
                    gzjlBean.setGZJT(GZJT);
                    gzjlBean.setGZQK(GZQK);
                    gzjlBean.setSFTZ(SFTZ);
                    list.add(gzjlBean);
                    gzjlService.save(gzjlBean);

                }
            }
        } catch (Exception e) {
            e.printStackTrace();

        }

    }


    private void httpSB(String GCID, String rwid) {
        Map<String, String> map = new HashMap<String, String>();
        try {
            map.put("APPID", StaticUtil.APPID);
            map.put("GCID", GCID);
            String response = ToolShowDemo.manageWebServices("GetJSJDRWSBXX", map);
            JSONObject json = JSON.parseObject(response);
            int result = json.getIntValue("result");
            if (result == 0) {
                String content = json.getString("description");
                ERRORINFO = "设备信息获取失败";
            } else {
                JSONArray value = json.getJSONArray("resultValue");
                List<SBBean> list = new ArrayList<>();

                for (int i = 0; i < value.size(); i++) {
                    JSONObject obj = value.getJSONObject(i);
                    SBBean bean = new SBBean();
                    String SBID = obj.getString("OBJ_ID");
                    String SBMC = obj.getString("SBMC");
                    String SBLX = obj.getString("SBLX");
                    String DYDJ = obj.getString("DYDJ");
                    String SCCJ = obj.getString("SCCJ");
                    String GCMC = obj.getString("GCMC");

                    bean.setGCID(GCID);
                    bean.setSB_ID(SBID);
                    bean.setSBMC(SBMC);
                    bean.setSBLX(SBLX);
                    bean.setDYDJ(DYDJ);
                    bean.setSCCJ(SCCJ);
                    bean.setGCMC(GCMC);
                    sbService.save(bean);
                }
                httpXZ(rwid);
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
    }


    private void httpGC(String GCID) {

        Map<String, String> map = new HashMap<String, String>();
        map.put("OBJ_ID", GCID);
        map.put("APPID", StaticUtil.APPID);
        String response = ToolShowDemo.manageWebServices("GetJSJDGCXX", map);

        JSONObject json = JSON.parseObject(response);
        int result = json.getIntValue("result");
        if (result == 0) {
            String content = json.getString("description");
            ERRORINFO = "工程信息获取失败";
        } else {
            JSONArray value = json.getJSONArray("resultValue");
            if (value.size() <= 0) {
            } else {
                JSONObject obj = value.getJSONObject(0);
                String GCMC = obj.getString("GCMC");
                String GCLX = obj.getString("GCLX");
                String DYDJ = obj.getString("DYDJ");
                GCBean bean = new GCBean();
                bean.setGC_ID(GCID);
                bean.setGCMC(GCMC);
                bean.setGCLX(GCLX);
                bean.setDYDJ(DYDJ);
                bean.setGCXZ("");
                gcService.save(bean);

            }
        }
    }


    private void httpXZ(String rwid) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("APPID", StaticUtil.APPID);
        map.put("HD_ID", rwid);
        String response = ToolShowDemo.manageWebServices("GetJSJDRWPJXZ", map);
        JSONObject json = JSON.parseObject(response);
        int result = json.getIntValue("result");
        if (result == 0) {
            String content = json.getString("description");
            ERRORINFO = "细则信息获取失败";
        } else {
            JSONArray value = json.getJSONArray("resultValue");
            List<XZBean> list = new ArrayList<>();
            XZBean xzBean;
            for (int i = 0; i < value.size(); i++) {
                JSONObject obj = value.getJSONObject(i);
                xzBean = new XZBean();
                String XZ_ID = obj.getString("XZ_ID");
                String XZMC = obj.getString("XZMC");
                xzBean.setZJ_ID(rwid + XZ_ID);
                xzBean.setRWID(rwid);
                xzBean.setXZ_ID(XZ_ID);
                xzBean.setXZMC(XZMC);
                xzBean.setHEGE("");
                xzBean.setBUHEGE("");
                xzBean.setBUSHEJI("");
                list.add(xzBean);
                xzService.save(xzBean);
                httpXM(XZ_ID, rwid);

            }


        }

    }

    private void httpXM(String xz_id, String rwid) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("APPID", StaticUtil.APPID);
        map.put("JDJD", JDJD);
        map.put("PJXZ_ID", xz_id);
        String response = ToolShowDemo.manageWebServices("GetJSJDJDXM", map);
        JSONObject json = JSON.parseObject(response);
        int result = json.getIntValue("result");
        if (result == 0) {
            String content = json.getString("description");
            ERRORINFO = "项目信息获取失败";
        } else {
            JSONArray value = json.getJSONArray("resultValue");
            List<XMBean> list = new ArrayList<>();
            XMBean xmBean;
            for (int i = 0; i < value.size(); i++) {
                JSONObject obj = value.getJSONObject(i);
                xmBean = new XMBean();
                String XMID = obj.getString("OBJ_ID");
                String XMMC = obj.getString("PJXMMC");
                String XMQZ = obj.getString("QZ");
                String JDYQ = obj.getString("JDYQ");

                xmBean.setXM_ID(XMID);
                xmBean.setXMMC(XMMC);
                xmBean.setQZ(XMQZ);
                xmBean.setJDYJ(JDYQ);
                xmBean.setXZID(xz_id);
                int jd = Integer.parseInt(JDJD);
                xmBean.setJDJD(StaticUtil.list_jd.get(jd - 1));
                list.add(xmBean);
                xmService.save(xmBean);
                httpYD(XMID, xz_id, rwid);
                xzService.updateXMSIZE(xz_id, value.size() + "");
            }
        }
    }


    private void httpYD(String xmid, String XZID, String rwid) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("APPID", StaticUtil.APPID);
        map.put("JDXM_ID", xmid);
        String response = ToolShowDemo.manageWebServices("GetJSJDJDYD", map);
        JSONObject json = JSON.parseObject(response);
        int result = json.getIntValue("result");

        if (result == 0) {
            ERRORINFO = "要点信息获取失败";
        } else {
            JSONArray value = json.getJSONArray("resultValue");
            List<YDBean> list = new ArrayList<>();
            YDBean ydBean;
            for (int i = 0; i < value.size(); i++) {
                JSONObject obj = value.getJSONObject(i);
                ydBean = new YDBean();
                String YDID = obj.getString("OBJ_ID");
                String YDMC = obj.getString("JDYDNR");

                ydBean.setXMID(xmid);
                ydBean.setYD_ID(YDID);
                ydBean.setYDMC(YDMC);
                ydBean.setXZID(XZID);
                ydBean.setOBJ_ID(YDID + rwid);
                ydBean.setRWID(rwid);
                list.add(ydBean);
                ydService.save(ydBean);
                list = ydService.findXZID(XZID);
                xzService.updateYDSIZE(XZID, list.size() + "");
            }
        }
    }

    private void intenView(View view) {
        tv_NameShow = (TextView) view.findViewById(R.id.tv_NameShow);
        layout_daiban = (LinearLayout) view.findViewById(R.id.layout_framgent_daiban);
        tv_number = (TextView) view.findViewById(R.id.tv_framgent_number);
        layout_daiban.setOnClickListener(this);

        list = ryrwService.findALLRW(StaticUtil.ISC_ID);
        String str = list.toString();
        tv_number.setText(list.size() + "");
        tv_NameShow.setText(StaticUtil.RYMC);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_framgent_daiban:
                Intent intent = new Intent();
                intent.setClass(getActivity(), ProjectListActivity.class);
                startActivityForResult(intent, 0);
                break;
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch (requestCode) {
            case 0:
                break;
            default:
                break;
        }
    }

    @Override
    public void fetchData() {
    }

    private String getZYLX(String numString) {
        String str[] = numString.split(",");
        List<String> tempList = Arrays.asList(str);
        String ZYLX = new String();
        for (int i = 0; i < tempList.size(); i++) {
            try {
                int zy = Integer.parseInt(tempList.get(i));
                String ZYLXname = StaticUtil.list_zy.get(zy - 1);
                ZYLX += ZYLXname;
                if (i < (tempList.size() - 1)) {
                    ZYLX += ",";
                }
            } catch (NumberFormatException e) {
                Log.e(TAG, "专业代码非数字：" + e);
            }
        }
        return ZYLX;
    }


    class MyAsynTask extends AsyncTask<String, Void, Void> {
        private View mView;

        public MyAsynTask(View view) {
            this.mView = view;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            DialogUtils.startProgressDialog(getActivity(), "正在加载");
            ERRORINFO = "";
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !ERRORINFO.equals("")) {
                Toast.makeText(getActivity(), ERRORINFO, Toast.LENGTH_SHORT).show();
            }
            intenView(mView);
            DialogUtils.stopProgressDialog();
        }

        @Override
        protected Void doInBackground(String... params) {
            http();
            return null;
        }
    }


}
